package com.jeewms.www.tms.ui.acitivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jeewms.www.tms.base.BaseBackActivity;
import com.jeewms.www.tms.bean.vm.Ginfo;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.base.BaseActivity;
import com.jeewms.www.tms.bean.listvm.DemoListVm;
import com.jeewms.www.tms.bean.vm.wmapiUtil.ResultDO;
import com.jeewms.www.tms.constance.Constance;
import com.jeewms.www.tms.util.EditChangedListener;
import com.jeewms.www.tms.util.GsonUtils;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.z_toast;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class Kcqr_Activity extends BaseBackActivity {
    private TextView btn = null;
    String tag ;
    ListView mLvGent;
    List<Ginfo> mginfo;
  //  String s = null;
//    String [] resid = { "A01001","A02101","A031001","A041001",
//    } ;

    GINfo madaper;
    private int titlebar_back;
    public static void show(Context context) {
        Intent intent = new Intent(context, Kcqr_Activity.class);
        context.startActivity(intent);

    }

    protected void initView() {
        super.initView();
        //  ButterKnife.bind(this);
        //  EventBus.getDefault().register(this);
        Logutil.print("测试System的使用LOGCAT");
        setTitle("库存查询");
        final EditText et_fangweima = (EditText) findViewById(R.id.et_fangweima);
        final EditText et_werks = (EditText) findViewById(R.id.et_werks);
        final EditText et_lgort = (EditText) findViewById(R.id.et_lgort);
        final EditText et_chuwei = (EditText)findViewById(R.id.et_chuwei);
      //  final EditText et_fangweima = (EditText)findViewById(R.id.et_fangweima);

      //  et_fangweima.addTextChangedListener(new EditChangedListener(et_fangweima));
       // et_werks.addTextChangedListener(new EditChangedListener(et_werks));

        btn = (TextView) findViewById(R.id.tv_search);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                z_toast ts = new z_toast();

                if (TextUtils.isEmpty(et_chuwei.getText().toString())&& TextUtils.isEmpty(et_fangweima.getText().toString())) {
                    ts.showToast("仓储号和物料号不能同时为空！",Kcqr_Activity.this);
                    return;
                }
//                if (TextUtils.isEmpty(et_lgort.getText().toString())) {
//                    ts.showToast("库位不能为空！",Kcqr_Activity.this);
//                    return;
//                }

                getData();
            }
        });
    }

    private void getData() {
//        String url = Constance.gettSapLsqdControllerURL() + "?username=" + SharedPreferencesUtil.getInstance(this).getKeyValue(Constance.SHAREP.LOGINNAME);
//        if (!StringUtil.isEmpty(etJiaojiedan.getText().toString()))
//            url += "&searchstr=" + etJiaojiedan.getText().toString();
//        if (!StringUtil.isEmpty(etFangweima.getText().toString()))
//            url += "&searchstr2=" + etFangweima.getText().toString();
        final EditText et_fangweima1 = (EditText) findViewById(R.id.et_fangweima);
        final EditText et_werks1 = (EditText) findViewById(R.id.et_werks);
        final EditText et_lgort1 = (EditText) findViewById(R.id.et_lgort);
        final EditText et_chuwei1 = (EditText) findViewById(R.id.et_chuwei);
       // String url = "http://172.17.210.197:8080/zzwms/rest/wmapi/getdoc/KCCX";
        String url = "http://192.168.200.100/zzwms/rest/wmapi/getdoc/KCCX"+"?searchstr1="
                +"&searchstr2="  + et_fangweima1.getText().toString()  +"&searchstr3="  + et_chuwei1.getText().toString()  +"&searchstr4="  + et_werks1.getText().toString()
                +"&searchstr5="  + et_lgort1.getText().toString();
        Logutil.print("url" + url);
        HTTPUtils.get(getActivity(), url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }

            @Override
            public void onResponse(String response) {
                Logutil.print("res==" + response);
                ResultDO vm = GsonUtils.parseJSON(response, ResultDO.class);
                mginfo = new ArrayList<Ginfo>();
               // Logutil.print("mginfo.size()1=");


                for(int i = 0;i<vm.getObj().size();i++) {
                    Ginfo bean = new Ginfo();
                    bean.setResid(vm.getObj().get(i).getWmX2());
                 //   bean.setMenge(vm.getObj().get(i).getX3());
                    bean.setMenge(vm.getObj().get(i).getWmX3());
                    bean.setBinId(vm.getObj().get(i).getWmX6());
                    bean.setMaktx(vm.getObj().get(i).getWmX8());
                    mginfo.add(bean);
                //    Logutil.print("mginfo.size()2="+mginfo.size());
                }
                initview();

            //    Logutil.print("mginfo.size()3="+mginfo.size());

            }
        });

    }

    @SuppressLint("WrongViewCast")
    private void initview() {
        mLvGent = (ListView)findViewById(R.id.lv_mylistview);
        madaper = new GINfo();
        mLvGent.setAdapter(madaper);

    }

//    private void initdata() {
//
////        mginfo = new ArrayList<Ginfo>();
////        for(int i = 0;i<4;i++) {
////            Ginfo bean = new Ginfo(resid[i]);
////            mginfo.add(bean);
////        }
//    }

    class GINfo extends BaseAdapter{


         @Override
         public int getCount() {
             return mginfo.size();
         }

         @Override
         public Ginfo getItem(int i) {

             return mginfo.get(i);
         }

         @Override
         public long getItemId(int i) {
             return i;
         }

         @Override
         public View getView(int i, View view, ViewGroup viewGroup) {

             View layout = View.inflate(Kcqr_Activity.this, R.layout.item_kcqr,null);
             //多行ITEM的显示，只要在实体类GINfo,java 增加变量,在这里增加变量参数
             TextView tvmatnr = layout.findViewById(R.id.tv_matnr);
             TextView tvmenge = layout.findViewById(R.id.tv_menge);
             TextView tvmaktx = layout.findViewById(R.id.tv_maktx);
             TextView tvbinId = layout.findViewById(R.id.tv_binId);
             Ginfo  bean = mginfo.get(i);
             tvmatnr.setText(bean.getResid());
             tvmenge.setText(bean.getMenge());
             tvmaktx.setText(bean.getMaktx());
             tvbinId.setText(bean.getBinId());
             return layout;
         }
     }


    private void parseDdta(String  result) throws JSONException {

        JSONObject JSONobject = new JSONObject(result);
        JSONArray  JSONArray = JSONobject.getJSONArray("list");

                    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    protected int getContentResId() {
        return R.layout.activity_kcqr;
    }
}
